import 'package:flutter/material.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photo_manager_image_provider/photo_manager_image_provider.dart';

class AppAssetImageWidget extends StatelessWidget {
  const AppAssetImageWidget({
    super.key,
    required this.entity,
    required this.option,
    required this.onPreview,
    required this.selected,
    this.idx = -1,
    this.showSelectMark = false,
    this.hasSend = false,
    this.onTap,
    this.onLongPress,
  });

  final AssetEntity entity;
  final ThumbnailOption option;
  final GestureTapCallback? onTap;
  final GestureTapCallback? onLongPress;
  final bool showSelectMark;
  final bool onPreview;
  final bool selected;
  final num idx;
  final bool hasSend;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: onTap,
        onLongPress: onLongPress,
        child: Stack(
          children: <Widget>[
            Positioned.fill(
              child: AssetEntityImage(
                entity,
                isOriginal: false,
                thumbnailSize: option.size,
                thumbnailFormat: option.format,
                fit: BoxFit.cover,
              ),
            ),
            Positioned(
              bottom: 4,
              right: 4,
              child: entity.type == AssetType.video
                  ? Image.asset(
                      "images/icon_video_asset.png",
                      width: 16,
                      height: 12,
                    )
                  : SizedBox(),
            ),
            Positioned.fill(
                child: Container(
                    color: onPreview ? Color(0x88ffffff) : Colors.transparent)),
            Positioned(
              top: 4,
              left: 4,
              child: showSelectMark
                  ? (idx != -1
                      ? indexWidget(idx + 1)
                      : Image.asset(
                          "images/icon_asset_unselected.png",
                          width: 18,
                          height: 18,
                        ))
                  : SizedBox(),
            ),
            Positioned(
              bottom: 4,
              left: 4,
              child: hasSend
                  ? Image.asset(
                      "images/icon_asset_has_send.png",
                      width: 18,
                      height: 18,
                    )
                  : SizedBox(),
            ),
          ],
        ));
  }

  Widget indexWidget(num idx) {
    return Container(
        width: 18,
        height: 18,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          color: Color(0xff3F74EF),
        ),
        child: Center(
            child: Text(
          idx.toString(),
          style: TextStyle(
            color: Colors.white,
            fontSize: 14,
          ),
          strutStyle: StrutStyle(
            height: 1.1,
            forceStrutHeight: true,
          ),
        )));
  }
}
